var fs = require('fs');
var path = require('path');
var pagePath = path.join(__dirname, '../device');
var imports = [];
var routesNames = [];
getRoutes(pagePath);
var fileString = "";
var fileString ="let alldevice=["
routesNames.forEach(function(item,index){
    fileString +=`require("${item.route}"),\n`
});
fileString+="]\n"
fileString+="export default alldevice"
fs.writeFileSync(path.join(__dirname, './Alldevice.js'), fileString);

function getRoutes(filePath, fileName, modulesName) {
    if (!modulesName) {
        modulesName = fileName;
    }

    var stat = fs.statSync(filePath);
    var isDir = stat.isDirectory();
    if (isDir) {
        var files = fs.readdirSync(filePath)
        if (files && files.length) {
            files.forEach(function (fn, index) {
                var fp = path.join(filePath, fn);
                getRoutes(fp, fn, modulesName);
            });
        }
    } else {
        if (fileName) {
            var pathName = filePath.replace(pagePath, '');
            var routesPath = '@/device'+pathName;
            if (process.platform.indexOf('win') >= 0) {
                routesPath = routesPath.replace(/\\/g, "\/");
            }
            pathName = pathName.replace('.js', '');
            pathName = pathName.replace('\\', '');
            routesNames.push({name:pathName.split('\\').join("."),route:routesPath});
        }
    }
}